package client;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import com.managment.alarm.CheckAlarm;
import com.managment.data.Media;
import com.managment.data.Task;
import com.managment.functions.add.AddFailed;
import com.managment.functions.add.AddTask;
import com.managment.functions.add.TaskAdded;


import communication.account.login.Login;
import communication.account.login.LoginAccomplished;
import communication.account.login.LoginFailed;

public class Client {
	//Konfiguracja klienta
	private static ObjectOutputStream oos;
	private static ObjectInputStream ois;
	private static String serverName = "89.75.191.16";
	private static int serverPort = 2223;
	private static Socket clientSocket;
	private static Object o;
	//komentarz do wyrzucenia
	


	private static void connectServer(){
		try {
			clientSocket = new Socket(serverName, serverPort);
			ois = new ObjectInputStream(clientSocket.getInputStream());
			oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.flush();
		} catch (UnknownHostException e) {
			System.out.println(e);
		} catch (IOException e) {
			System.out.println(e);
		}		
	}

	private static void initStreams(){
		//Ustawiam stałe wejście i wyjście na naszym sockecie
		try {
			oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.flush();
			ois = new ObjectInputStream(clientSocket.getInputStream());
		} catch (IOException e) {
			System.err.println(e);
		}		
	}
	
    public static void main(String[] args) {
    	connectServer();
    	//initStreams();
		Date now = new Date();
		System.out.println("H: "+now.getHours());
		System.out.println("M: "+now.getMinutes());
        int ch;
        //Dopóki mamy połączenie z serwerem możemy wpisywać komendy na konsoli
		while (clientSocket.isConnected()) {
			System.out.print("meetp$ ");
            StringBuffer sb = new StringBuffer();
            try {
				while ( (ch = System.in.read()) != 13) {
				    sb.append( (char) ch );
				}
			} catch (IOException e) {
				System.err.println(e);
			}
			if (sb.toString().trim().split(" ")[0].equals("login")){
				System.out.println("LOGIN");
				 String [] tmp = sb.toString().trim().split(" ");
				 if(tmp.length > 1){
					 System.out.println("2");
					 Login login = new Login();
					 login.setPassword(tmp[1]);
					 try {
						oos.writeObject(login);
						System.out.println("2");
						oos.flush();
						Object o = ois.readObject();
						System.out.println("2");
						if(o instanceof LoginAccomplished){
							System.out.println("Zalogowałeś się");
						}
						else if(o instanceof LoginFailed){
							System.out.println("Nie udało się zalogować");
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				 }
			}
			else if (sb.toString().trim().split(" ")[0].equals("media")){
				 String [] tmp = sb.toString().trim().split(" ");
				 Task task = new Task(null,null,null);
				 
				 if(tmp[1].equals("bathroom")){
					 task.setMedia(Media.BATHROOMLIGHT);
				 }
				 else if(tmp[1].equals("kitchen")){
					 task.setMedia(Media.KITCHENLIGHT);
				 }
				 else if(tmp[1].equals("room")){
					 task.setMedia(Media.ROOMLIGHT);
				 }
				 else if(tmp[1].equals("temp")){
					 task.setMedia(Media.HEATTER);
				 }
				 task.setState(tmp[2]);
				 try {
					 AddTask newTask = new AddTask();
					 newTask.setTask(task);
						oos.writeObject(newTask);
						System.out.println("2");
						oos.flush();
						Object o = ois.readObject();
						System.out.println("2");
						if(o instanceof TaskAdded){
							System.out.println("Zalogowałeś się");
						}
						else if(o instanceof AddFailed){
							System.out.println("Nie udało się zalogować");
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				 
			}	
			else if (sb.toString().trim().split(" ")[0].equals("alarm")){
				String [] tmp = sb.toString().trim().split(" ");
				try {
					 
				System.out.println("Po kolei");
				oos.writeObject(new CheckAlarm());
				oos.writeObject(new CheckAlarm());
				oos.writeObject(new CheckAlarm());
				oos.writeObject(new CheckAlarm());
				oos.writeObject(new CheckAlarm());
				Object o = ois.readObject();
				System.out.println(o.getClass());
				o = ois.readObject();
				System.out.println(o.getClass());
				o = ois.readObject();
				System.out.println(o.getClass());
				o = ois.readObject();
				System.out.println(o.getClass());
				o = ois.readObject();
				System.out.println(o.getClass());
				 
				System.out.println("Naprzemian");
				oos.writeObject(new CheckAlarm());
				o = ois.readObject();
				System.out.println(o.getClass());
				oos.writeObject(new CheckAlarm());
				o = ois.readObject();
				System.out.println(o.getClass());
				oos.writeObject(new CheckAlarm());
				o = ois.readObject();
				System.out.println(o.getClass());
				oos.writeObject(new CheckAlarm());
				o = ois.readObject();
				System.out.println(o.getClass());
				oos.writeObject(new CheckAlarm());
			    o = ois.readObject();
			    System.out.println(o.getClass());
				 
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				 
			}	 
			else{
            	sb = null;
            	System.out.println("BUBA");
			}
        }
        System.out.println("Disconnected from meetpServer");
    }
}
